package view;




import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;

import model.drawPanel.DrawNetModel;
import model.roomManagement.Implementation.LoginModel;
import model.roomManagement.Implementation.RoomManageModel;
import model.roomManagement.Interface.ILoginModel;
import model.roomManagement.Interface.IRoomManageModel;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import controller.fshare.ClientFileShareController;
import controller.fshare.Interface.ClientFileShareControl;
import controller.roomManagement.Implementation.LoginController;
import controller.roomManagement.Implementation.RoomManageController;
import controller.roomManagement.Interface.ILoginController;
import controller.roomManagement.Interface.IRoomManageController;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;

public class LoginView {
	LoginController loginController;
	LoginModel loginModel;

	public LoginView(ILoginController loginController, ILoginModel loginModel) {
		this.loginController=(LoginController)loginController;
		this.loginModel=(LoginModel)loginModel;
	}
	
//*************************************************************************************************
    protected Shell shlGms;
    private Display display;
	private Text text_ID;
	private Text text_password;
	private ProgressBar progressBar;
	private Label label_ID;
	private Label label_password;
	private Button button_login;
	private Button button_cancel;
	private Label label_ing;
	
	private int shell_x;
	private int shell_y;
	private ToolBar toolBar;
	private ToolItem tltmip;
	
	private IpComposite ic;
	/*private static int MAX_SAVEDID = 5;
	private String[][] id_password = new String [MAX_SAVEDID][2];*/
	
	private File file;
	private String read = "";
	public LoginView(){
		
	}
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LoginView window = new LoginView();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
        
		//读取本地帐号和密码的操作**************************************************************************************
		//未完待续。。。
		//读取本地帐号和密码的操作***结束********************************************************************************
		
		//读取txt文件操作*************************************************************************************
		file = new File("documents\\IP.txt");
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			read = br.readLine();		
			br.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//读取txt文件操作***结束*************************************************************************************
		
		
		display = Display.getDefault();
		createContents();
		shlGms.open();
		shlGms.layout();
		while (!shlGms.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		
		//获取屏幕高度和宽度
        int screenH = Toolkit.getDefaultToolkit().getScreenSize().height;
        int screenW = Toolkit.getDefaultToolkit().getScreenSize().width;
		
		shlGms = new Shell(SWT.CLOSE | SWT.MIN);
		shlGms.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent arg0) {
				//ic.dispose();			
				try {
					BufferedWriter bw = new BufferedWriter(new FileWriter("documents/IP.txt"));
					bw.write(ic.getText());				
					bw.close();
				} catch (IOException ioe) {
					// TODO Auto-generated catch block
					ioe.printStackTrace();
				}
			}
		});
		shlGms.setImage(SWTResourceManager.getImage(LoginView.class, "/img/GMS.png"));
		shlGms.setLocation(screenW-300, screenH-650);
		shlGms.setSize(280, 505);
		shlGms.setText("GMS\u767B\u5F55");
		
		text_ID = new Text(shlGms, SWT.BORDER);
		text_ID.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ic.setVisible(false);
			}
		});
		text_ID.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				ic.setVisible(false);
			}
		});
		text_ID.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				char c = e.character;
				if(!(Character.isDigit(c) || e.keyCode==8 || e.keyCode==13)){
					e.doit = false;
				}
				if(e.keyCode == 13){
					text_password.forceFocus();
				}
			}
		});
		text_ID.setBounds(26, 200, 222, 25);
		
		text_password = new Text(shlGms, SWT.BORDER | SWT.PASSWORD);
		text_password.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ic.setVisible(false);
			}
		});
		text_password.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				ic.setVisible(false);
			}
		});
		text_password.setBounds(26, 265, 222, 25);
		//密码栏键盘确定键的响应
		text_password.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode == 13){
					label_ID.setVisible(false);
					label_password.setVisible(false);
					text_ID.setVisible(false);
					text_password.setVisible(false);
					button_login.setVisible(false);
					toolBar.setVisible(false);
					ic.setVisible(false);
					
					progressBar.setVisible(true);
					
					//System.out.println(ic.getText());
					loginController.setServerIp(ic.getText());
					DrawNetModel.serverIP = ic.getText();
					
					
					if(text_ID.getText().equals("") || text_password.getText().equals("")){
						//MessageDialog.openError(shlGms, "登陆错误", "帐号或密码不能为空！");
						progressBar.setVisible(false);
						
						button_cancel.setVisible(true);
						label_ing.setVisible(true);
						label_ing.setText("帐号或密码不能为空");
					}
					else{			
						int ID = Integer.parseInt(text_ID.getText());
						String password = text_password.getText();
						
						try {
							if(loginController.login(ID, password) == 1){
								shell_x = shlGms.getLocation().x;
								shell_y = shlGms.getLocation().y;
								shlGms.dispose();
								//shlGms.setVisible(false);
								IRoomManageModel RMM = new RoomManageModel(loginModel.getUser(),loginModel.getServer(),new Socket());
								IRoomManageController RMC = new RoomManageController(RMM);
								
								RoomManageView rmv = new RoomManageView(RMC,RMM,shell_x,shell_y);
								rmv.open();						
							}
							else if(loginController.login(ID, password) == 0){
								progressBar.setVisible(false);
								
								button_cancel.setVisible(true);
								label_ing.setVisible(true);
								label_ing.setText("帐号或密码错误");
							}
							else if(loginController.login(ID, password) == 2){
								progressBar.setVisible(false);
								
								button_cancel.setVisible(true);
								label_ing.setVisible(true);
								label_ing.setText("该用户已在别处登录");
							}
							
						} catch (model.roomManagement.Implementation.ConnectException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							progressBar.setVisible(false);
							
							button_cancel.setVisible(true);
							label_ing.setVisible(true);
							label_ing.setText("网络错误，与服务器未能成功连接");
						}					
					}			
				}
			}
		});
		Composite composite = new Composite(shlGms, SWT.NONE);
		composite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				ic.setVisible(false);
			}
		});
		composite.setBounds(0, 0, 275, 480);
		composite.setBackgroundImage(SWTResourceManager.getImage(LoginView.class, "/img/背景有字体_275_480.jpg"));
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		
		label_ID = new Label(composite, SWT.NONE);
		label_ID.setBounds(26, 180, 61, 17);
		label_ID.setText("\u5E10\u53F7\uFF1A");
		
		label_password = new Label(composite, SWT.NONE);
		label_password.setBounds(26, 245, 61, 17);
		label_password.setText("\u5BC6\u7801\uFF1A");
		
		progressBar = new ProgressBar(composite, SWT.SMOOTH | SWT.INDETERMINATE);
		progressBar.setBounds(26, 235, 222, 10);
		progressBar.setVisible(false);
		
		button_login = new Button(composite, SWT.NONE);
		button_login.setToolTipText("点击登录");
		button_login.setBounds(97, 334, 80, 27);
		button_login.setText("\u767B\u5F55");
		
		button_cancel = new Button(composite, SWT.NONE);
		button_cancel.setBounds(97, 265, 80, 27);
		button_cancel.setText("重试");
		button_cancel.setVisible(false);
		
		label_ing = new Label(composite, SWT.NONE);
		label_ing.setAlignment(SWT.CENTER);
		label_ing.setBounds(26, 200, 222, 17);
		//label_ing.setText("正在登录中");
		label_ing.setVisible(false);
		
		toolBar = new ToolBar(composite, SWT.FLAT | SWT.RIGHT);
		toolBar.setBounds(26, 445, 85, 28);
		
		tltmip = new ToolItem(toolBar, SWT.NONE);
		tltmip.setText("服务器IP");		
		tltmip.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(ic.getVisible()){
					ic.setVisible(false);
				}
				else{
					ic.setVisible(true);
				}
			}
		});
		tltmip.setImage(SWTResourceManager.getImage(LoginView.class, "/img/IP_22.png"));
		
		ic = new IpComposite(composite, SWT.BORDER);
		ic.setBounds(26, 422, 160, 23);
		ic.setVisible(false);
		try {
			ic.setText(read);
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		//登录按钮的响应****************************************************
		button_login.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//System.out.println(ic.getText());
				label_ID.setVisible(false);
				label_password.setVisible(false);
				text_ID.setVisible(false);
				text_password.setVisible(false);
				button_login.setVisible(false);
				toolBar.setVisible(false);
				ic.setVisible(false);
				
				progressBar.setVisible(true);
				loginController.setServerIp(ic.getText());
				DrawNetModel.serverIP = ic.getText();
				
				
				if(text_ID.getText().equals("") || text_password.getText().equals("")){
					//MessageDialog.openError(shlGms, "登陆错误", "帐号或密码不能为空！");
					progressBar.setVisible(false);
					
					button_cancel.setVisible(true);
					label_ing.setVisible(true);
					label_ing.setText("帐号或密码不能为空");
				}
				else{			
					int ID = Integer.parseInt(text_ID.getText());
					String password = text_password.getText();
					
					try {
						if(loginController.login(ID, password) == 1){
							shell_x = shlGms.getLocation().x;
							shell_y = shlGms.getLocation().y;
							shlGms.dispose();
							//shlGms.setVisible(false);
							IRoomManageModel RMM = new RoomManageModel(loginModel.getUser(),loginModel.getServer(),new Socket());
							IRoomManageController RMC = new RoomManageController(RMM);
							
							RoomManageView rmv = new RoomManageView(RMC,RMM,shell_x,shell_y);
							rmv.open();						
						}
						else if(loginController.login(ID, password) == 0){
							progressBar.setVisible(false);
							
							button_cancel.setVisible(true);
							label_ing.setVisible(true);
							label_ing.setText("帐号或密码错误");
						}
						else if(loginController.login(ID, password) == 2){
							progressBar.setVisible(false);
							
							button_cancel.setVisible(true);
							label_ing.setVisible(true);
							label_ing.setText("该用户已在别处登录");
						}
						
					} catch (model.roomManagement.Implementation.ConnectException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						progressBar.setVisible(false);
						
						button_cancel.setVisible(true);
						label_ing.setVisible(true);
						label_ing.setText("网络错误，与服务器未能成功连接");
					}					
				}			
			}
		});
		//*************************************************************
		
		//****************************************************
		button_cancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				label_ID.setVisible(true);
				label_password.setVisible(true);
				text_ID.setVisible(true);
				text_password.setVisible(true);
				button_login.setVisible(true);
				toolBar.setVisible(true);
				ic.setVisible(false);
				
				progressBar.setVisible(false);
				
				button_cancel.setVisible(false);
				label_ing.setVisible(false);
			}
		});
		//*************************************************************
		
		
		
	}
	
	
}
